package com.cennavi.minemapdemo.net;


import android.text.TextUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 网络请求提供者
 * Created by lijingru on 2018/3/2.
 */

public class HttpProvider {


    /**
     * 单例模式
     */
    private volatile static HttpProvider provider;

    public static synchronized HttpProvider getInstance() {
        if (provider == null) {
            provider = new HttpProvider();
        }
        return provider;
    }

    /**
     * 通用方法 文件 post
     *
     * @param url
     * @param mapParams
     * @param pathName
     * @param fileName
     * @return
     */
    @android.support.annotation.Nullable
    public String getOkHttpString(String url, Map<String, Object> mapParams, String pathName, String fileName) {
        String result = null;
        boolean is_ret = false;
        int tryCount = 0;
        String resultStr = null;
        while (!is_ret) {
            Response response = null;
            try {

                if (!TextUtils.isEmpty(pathName) && !TextUtils.isEmpty(fileName)) {
                    response = OkHttp3Utils.doFileT(url, pathName, fileName, "upfile", mapParams);
                } else {
                    response = OkHttp3Utils.doPostT(url, mapParams);
                }

                if (response.isSuccessful()) {
                    resultStr = response.body().string();
                }
                is_ret = response.isSuccessful();
                tryCount++;
                if (tryCount > 3)
                    break;
            } catch (IOException e) {
                e.printStackTrace();
                tryCount++;
                if (tryCount > 3)
                    break;
            }
            if (response != null) {
                if (response.body() != null)
                    response.body().close();
                response.close();
            }
        }
        if (is_ret) {
            result = resultStr;
        }
        return result;
    }

    /**
     * 通用方法  map post
     *
     * @param url
     * @param mapParams
     * @return
     */
    @android.support.annotation.Nullable
    public String getOkHttpString(String url, Map<String, Object> mapParams) {
        String result = null;
        boolean is_ret = false;
        int tryCount = 0;
        String resultStr = null;
        while (!is_ret) {
            Response response = null;
            try {
                response = OkHttp3Utils.doPostT(url, mapParams);
                if (response.isSuccessful()) {
                    resultStr = response.body().string();
                }
                is_ret = response.isSuccessful();
                tryCount++;
                if (tryCount > 3)
                    break;
            } catch (IOException e) {
                e.printStackTrace();
                tryCount++;
                if (tryCount > 3)
                    break;
            }
            if (response != null) {
                if (response.body() != null)
                    response.body().close();
                response.close();
            }
        }
        if (is_ret) {
            result = resultStr;
        }
        return result;
    }

    /**
     * 通用方法  json post
     *
     * @param url
     * @param json
     * @return
     */
    @android.support.annotation.Nullable
    public String getOkHttpString(String url, String json) {
        String result = null;
        boolean is_ret = false;
        int tryCount = 0;
        String resultStr = null;
        while (!is_ret) {
            Response response = null;
            try {
                response = OkHttp3Utils.doPostT(url, json);
                if (response.isSuccessful()) {
                    resultStr = response.body().string();
                }
                is_ret = response.isSuccessful();
                tryCount++;
                if (tryCount > 3)
                    break;
            } catch (IOException e) {
                e.printStackTrace();
                tryCount++;
                if (tryCount > 3)
                    break;
            }
            if (response != null) {
                if (response.body() != null)
                    response.body().close();
                response.close();
            }
        }
        if (is_ret) {
            result = resultStr;
        }
        return result;
    }

    /**
     * 通用方法 get
     *
     * @param url
     * @return
     */
    @android.support.annotation.Nullable
    public String getOkHttpString(String url) {
        String result = null;
        boolean is_ret = false;
        int tryCount = 0;
        String resultStr = null;
        while (!is_ret) {
            Response response = null;
            try {
                response = OkHttp3Utils.doGetT(url);
                if (response.isSuccessful()) {
                    resultStr = response.body().string();
                }
                is_ret = response.isSuccessful();
                tryCount++;
                if (tryCount > 3)
                    break;
            } catch (IOException e) {
                e.printStackTrace();
                tryCount++;
                if (tryCount > 3)
                    break;
            }
            if (response != null) {
                if (response.body() != null)
                    response.body().close();
                response.close();
            }
        }
        if (is_ret) {
            result = resultStr;
        }
        return result;
    }

}
